/*
 * Copyright (C) 2021 ShangHai Quicktron Intelligent Technology Co.,Ltd
 * All Rights Reserved.
 * 本软件为上海快仓智能科技开发研制。未经本公司正式书面同意，其他任何个人、团体
 * 不得使用、复制、修改或发布本软件.
 * 版权所有 翻版必究
 */

package ATOPSTD;

import java.util.ArrayList;
import java.util.List;

/**
 * The type Able socket api.
 */
public class AbleSocketAPI {
    /**
     * The constant DOT.
     */
    final static byte DOT = 0;
    /**
     * The constant ALWAYS_LIGHTING.
     */
    final static short ALWAYS_LIGHTING = 0;
    /**
     * The constant KEEP_FLASH.
     */
    final static short KEEP_FLASH = -1;
    /**
     * The constant STAY_OFF.
     */
    final static short STAY_OFF = -3;
    /**
     * The constant UNLOCK.
     */
    final static byte UNLOCK = 0;
    /**
     * The constant LOCK.
     */
    final static byte LOCK = 1;
    /**
     * The constant CONFIRM_BUTTON.
     */
    final static byte CONFIRM_BUTTON = 1;
    /**
     * The constant deviceIds.
     */
    public static List<Integer> deviceIds = new ArrayList<Integer>();
    private static AbleSocketClient ableSocketClient;
    private static int turnOnLed = 0;
    private static int turnOffLed = 1;
    private static int turnOnLight = 2;
    private static int turnOffLight = 3;
    private static int buzz = 4;
    private static int unbuzz = 5;
    private static int falsh = 11;
    private static int lockOrUnlock = 31;
    private static String lockNumber = "\\03\\0\\55";
    private static String unlockNumber = "\\03\\59\\55";

    /**
     * 打开API,连接设备
     *
     * @param ip the ip
     * @return the boolean
     * @throws Exception the exception
     */
    public static boolean openAPI(String ip) throws Exception {
        ableSocketClient = new AbleSocketClient(ip);
        //打开api
        int num = 0;
        try {
            num = ableSocketClient.connect();
        } catch (Exception e) {
        }

        if (num <= 0) {
            System.out.println("API Open Failed");
            return false;
        } else {
        }
        return true;
    }

    /**
     * 关闭API和设备
     */
    public static void close() {
        int num = ableSocketClient.disconnect();
    }

    /**
     * 打开Led
     *
     * @param lightPosition the light position
     * @param num           the num
     */
    public static void turnOnLed(int lightPosition, int num) {
        ableSocketClient.sendCommand(turnOnLed, lightPosition, num);
        ableSocketClient.sendCommand(lockOrUnlock, lightPosition, lockNumber);
    }

    /**
     * 打开Led
     *
     * @param lightPosition the light position
     */
    public static void unlockLed(int lightPosition) {
        ableSocketClient.sendCommand(lockOrUnlock, lightPosition, unlockNumber);
    }

    /**
     * 关闭Led
     *
     * @param lightPosition the light position
     */
    public static void turnOffLed(int lightPosition) {
        ableSocketClient.sendCommand(turnOffLed, lightPosition);
    }

    /**
     * 打开灯
     *
     * @param lightPosition the light position
     * @param num           the num
     */
    public static void turnOn(int lightPosition, int num) {
        ableSocketClient.sendCommand(turnOnLight, lightPosition, num);
        ableSocketClient.sendCommand(lockOrUnlock, lightPosition, lockNumber);
    }

    /**
     * 关闭灯
     *
     * @param lightPosition the light position
     */
    public static void turnOff(int lightPosition) {
        ableSocketClient.sendCommand(turnOffLight, lightPosition);
    }

    /**
     * Recieve message tag rcv msg.
     *
     * @return tag rcv msg
     */
    public static TagRcvMsg recieveMessage() {
        TagRcvMsg tagRcvMsg = new TagRcvMsg();
        int num = ableSocketClient.receiveMsg(tagRcvMsg);
        if (num > 0) {
            if (tagRcvMsg.subcmd == 6 || tagRcvMsg.subcmd == 7) {
                return tagRcvMsg;
            }
        }
        return null;
    }

    /**
     * Beep.
     *
     * @param lightPosition the light position
     * @param mode          the mode
     * @throws Exception the exception
     */
    public static void beep(int lightPosition, int mode) throws Exception {
        ableSocketClient.sendCommand(mode == 1 ? buzz : unbuzz, lightPosition);
    }

    /**
     * Flash.
     *
     * @param lightPosition the light position
     */
    public static void flash(int lightPosition) {
        ableSocketClient.sendCommand(falsh, lightPosition);
    }

    /**
     * Is server ok boolean.
     *
     * @return the boolean
     */
    public static boolean isServerOk() {
        return ableSocketClient.isServerOk();
    }
}
